<?php

namespace app\result;

use app\model\AccessLog;
use app\model\SysStatistics;
use app\model\user\Cashflows;
use app\model\user\User;

class SysStatisticsResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new SysStatistics();
    }

    public static function create(): array
    {
        $ckey = 'sys_statistics_' . date('Ymd');
        if (cache($ckey)) {
            return self::success_return('已经进行统计');
        }
        cache($ckey, 1, 3600);
        $model = new SysStatistics();

        $date = date('Y-m-d', strtotime("-1 day"));//昨天

        if ($model->where('date', $date)->value('id')) {
            return self::success_return('已经进行统计');
        }
        $dateStart = $date . ' 00:00:00';
        $dateEnd = $date . ' 23:59:59';
        $year = date('Y', strtotime($date));
        $month = date('m', strtotime($date));
        $day = date('d', strtotime($date));
        $modelUser = new User();
        $modelCashflows = new Cashflows();
        $reg_user_count = $modelUser->data_count([['created_at', 'between', [$dateStart, $dateEnd]]]);
        $active_user_count = $modelUser->data_count([['last_active_time', 'between', [$dateStart, $dateEnd]]]);
        $amount_count = $modelCashflows->data_count([['created_at', 'between', [$dateStart, $dateEnd]]]);//当天记录的记账笔数
        $amount_total = $modelCashflows->data_sum([['created_at', 'between', [$dateStart, $dateEnd]]], 'amount');
        $amount_in = $modelCashflows->data_sum([['created_at', 'between', [$dateStart, $dateEnd]], ['type', '=', 10]], 'amount');
        $amount_out = $modelCashflows->data_sum([['created_at', 'between', [$dateStart, $dateEnd]], ['type', '=', 20]], 'amount');
        $api_count = (new AccessLog())->data_count([['created_at', 'between', [$dateStart, $dateEnd]]]);
        $baseData = [
            'date' => $date,
            'year' => $year,
            'month' => $month,
            'day' => $day,
            'reg_user_count' => $reg_user_count,
            'active_user_count' => $active_user_count,
            'amount_count' => $amount_count,
            'amount_total' => $amount_total,
            'amount_in' => $amount_in,
            'amount_out' => $amount_out,
            'api_count' => $api_count,
        ];

        //兼容手动二次执行
        if ($id = $model->where('date', $date)->value('id')) {
            $model->where('id', $id)->update($baseData);
        } else {
            $res = $model->save($baseData);
            if (!$res) {
                cache($ckey, null);
                return self::error_return('保存失败');
            }
        }

        //bs_log($baseData, 'SysStatistics', 'statistics.log');

        return self::success_return('success', $baseData);
    }

    public function list($args)
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $date_range = $args['date_range'] ?? [];
        $where = [];
        if (!empty($args['keyword'])) {
            $where[] = ['date', 'like', '%' . trim($args['keyword']) . '%'];
        }
        if ($date_range) {
            $where[] = ['date', 'between', $date_range];
        }
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', 'id desc', $page, $pageSize);
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data
        ]);
    }
}
